class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if (nums.size() < 3) return nums.size();
        int r = 2;
        int l = 2;
        while (r < nums.size()){
            if (nums[l - 2] == nums[r]){
                r++;
            }
            else{
                nums[l] = nums[r];
                l++;
                r++;
            }
        }
        return l;
    }
};